package co.yixiang.modules.supplier.rest;

import co.yixiang.aop.log.Log;
import co.yixiang.modules.supplier.domain.SupplierUserApply;
import co.yixiang.modules.supplier.service.SupplierUserApplyService;
import co.yixiang.modules.supplier.service.SupplierUserService;
import co.yixiang.modules.supplier.service.dto.SupplierUserApplyDto;
import co.yixiang.modules.supplier.service.dto.SupplierUserApplyQueryCriteria;
import co.yixiang.modules.supplier.service.mapper.SupplierUserApplyMapper;
import co.yixiang.utils.SecurityUtils;
import com.alibaba.fastjson.JSONObject;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
* @author Walert
* @date 2021-03-27
*/
@Api(tags = "用户供应商申请管理")
@RestController
@RequestMapping("/api/supplierUserApply")
public class SupplierUserApplyController {

    private final SupplierUserApplyService supplierUserApplyService;
    private final SupplierUserService supplierUserService;
    private final SupplierUserApplyMapper userApplyMapper;

    public SupplierUserApplyController(SupplierUserApplyService supplierUserApplyService,SupplierUserService supplierUserService,SupplierUserApplyMapper userApplyMapper) {
        this.supplierUserApplyService = supplierUserApplyService;
        this.supplierUserService = supplierUserService;
        this.userApplyMapper = userApplyMapper;
    }

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('supplierUserApply:list')")
    public void download(HttpServletResponse response, SupplierUserApplyQueryCriteria criteria) throws IOException {
        supplierUserApplyService.download(supplierUserApplyService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询用户供应商申请")
    @ApiOperation("查询用户供应商申请")
    @PreAuthorize("@el.check('supplierUserApply:list')")
    public ResponseEntity<Object> getSupplierUserApplys(SupplierUserApplyQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(supplierUserApplyService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增用户供应商申请")
    @ApiOperation("新增用户供应商申请")
    @PreAuthorize("@el.check('supplierUserApply:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody SupplierUserApply resources){
        return new ResponseEntity<>(supplierUserApplyService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改用户供应商申请")
    @ApiOperation("修改用户供应商申请")
    @PreAuthorize("@el.check('supplierUserApply:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody SupplierUserApply resources){
        supplierUserApplyService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除用户供应商申请")
    @ApiOperation("删除用户供应商申请")
    @PreAuthorize("@el.check('supplierUserApply:del')")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody Long[] ids) {
        supplierUserApplyService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }


    @Log("新增用户供应商申请")
    @ApiOperation("新增用户供应商申请")
    @PreAuthorize("@el.check('supplierUserApply:audit')")
    @PostMapping(value = "/audit")
    public ResponseEntity<Object> auditUser(@RequestBody JSONObject params) {
        Long applyId = params.getLong("id");
        SupplierUserApplyDto byId = supplierUserApplyService.findById(applyId);
        Integer auditStatus = params.getInteger("auditStatus");
        Integer level = params.getInteger("level");
        String nopassReason = params.getString("nopassReason");
        byId.setAuditStatus(auditStatus);
        byId.setNopassReason(nopassReason);
        byId.setUpdateTime(System.currentTimeMillis());
        byId.setUpdateUser(SecurityUtils.getUsername());
        supplierUserApplyService.update(userApplyMapper.toEntity(byId));
        if(auditStatus == 2){
            supplierUserService.create(byId.getUserId(), byId.getUserName(), byId.getCompanyName(), level);
        }
        return new ResponseEntity<>(HttpStatus.OK);
    }
}
